package conMgr.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import conMgr.model.User;
import conMgr.model.UserType;


/**
 * This class verify the user name with database.
 * 
 * @author Soon Bang.
 */
public class LoginView extends JFrame implements ActionListener
{
	
	/**
	 * The serialization ID for this class.
	 */
	private static final long serialVersionUID = -5283145887128085617L;

	/**
	 * The width of the login field.
	 */
	private static final int LOGIN_FIELD_WIDTH = 12;
	  
	/**
	 * The user name text.
	 */
	private static final String USERNAME = "Username: ";
	  
	/**
	 * The text of login button
	 */
	private static final String LOGIN = "Login";
	  
	/**
	 * The button of login.
	 */
	private final JButton my_login_button;
	  
	/**
	 * The label of user name field.
	 */
	private final JLabel my_user_label;
	  
	/**
	 * The text field of user name.
	 */
	private final JTextField  my_user_field;
	
	/**
	 * The panel of login.
	 */
	private final JPanel my_login_panel;
	/**
	 * The panel of user type button.
	 */
	private final JPanel my_button_panel;
	/**
	 * The Author select button.
	 */
	private final JRadioButton my_author_btn;
	/**
	 * The Reviewer select button.
	 */
	private final JRadioButton my_reviewer_btn;
	/**
	 * The SubprogramChair select button.
	 */
	private final JRadioButton my_spchair_btn;
	/**
	 * The ProgramChair select button.
	 */
	private final JRadioButton my_pchair_btn;
	/**
	 * The Administrator select button.
	 */
	private final JRadioButton my_admin_btn;
	/**
	 * The user.
	 */
	private User my_user;

	/**
	 * Frame of user Login.
	 */
	public LoginView()
	{
		super("Conference Manager Login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		my_user_label = new JLabel(USERNAME, JLabel.CENTER);
		my_user_field = new JTextField(LOGIN_FIELD_WIDTH);
		my_user_field.requestFocus();
		my_user_field.addActionListener(this);


		my_login_button = new JButton(LOGIN);
		
		my_login_panel = new JPanel();
		my_login_panel.add(my_user_label, BorderLayout.WEST);
		my_login_panel.add(my_user_field, BorderLayout.CENTER);
		my_login_panel.add(my_login_button, BorderLayout.EAST);
		
		my_author_btn = new JRadioButton(UserType.AUTHOR.getFriendlyName());
		my_author_btn.setSelected(true);
		my_reviewer_btn = new JRadioButton(UserType.REVIEWER.getFriendlyName());
		my_spchair_btn = new JRadioButton(UserType.SUBPROGRAM_CHAIR.getFriendlyName());
		my_pchair_btn  = new JRadioButton(UserType.PROGRAM_CHAIR.getFriendlyName());
		my_admin_btn  = new JRadioButton(UserType.ADMIN.getFriendlyName());
		
		ButtonGroup group = new ButtonGroup();
		group.add(my_author_btn);
		group.add(my_reviewer_btn);
		group.add(my_spchair_btn);
		group.add(my_pchair_btn);
		group.add(my_admin_btn);
		
		my_button_panel = new JPanel();
		my_button_panel.add(my_author_btn, BorderLayout.SOUTH);
		my_button_panel.add(my_reviewer_btn, BorderLayout.SOUTH);
		my_button_panel.add(my_spchair_btn, BorderLayout.SOUTH);
		my_button_panel.add(my_pchair_btn, BorderLayout.SOUTH);
		my_button_panel.add(my_admin_btn, BorderLayout.SOUTH);
		
		my_login_button.addActionListener(this);
		
		add(my_login_panel, BorderLayout.CENTER);
		add(my_button_panel, BorderLayout.SOUTH);
		
	    pack();
	    setLocationRelativeTo(null);
	}
	
	/**
	 *  The action of Login button.	
	 */
	public void actionPerformed(ActionEvent e)
	{
		String username = my_user_field.getText();

		if (checkUser(username))
		{
			dispose();
			MainView mv = new MainView(my_user);
			mv.setVisible(true); 
		}
		else
		{
			JOptionPane.showMessageDialog(this,"Incorrect username or Login Type",
                    "Error",JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * Check the input user name is exist in user database.  
	 * @param the_user The input user name
	 * @return true If user name is exist otherwise false.
	 */
	public boolean checkUser(String the_user)
	{
		for (User user : User.getAllUsers())
		{
			if (my_author_btn.isSelected() && user.getName().equals(the_user)
					&& user.getType().equals(UserType.AUTHOR))
			{
		        User.setCurrentUser(user);
		        my_user = user;
				return true;
			}
			else if (my_reviewer_btn.isSelected() && user.getName().equals(the_user)
					&& user.getType().equals(UserType.REVIEWER))
			{
		        User.setCurrentUser(user);
		        my_user = user;
				return true;
			}
			else if (my_spchair_btn.isSelected() && user.getName().equals(the_user)
					&& user.getType().equals(UserType.SUBPROGRAM_CHAIR))
			{
		        User.setCurrentUser(user);
		        my_user = user;
				return true;
			}
			else if (my_pchair_btn.isSelected() && user.getName().equals(the_user)
					&& user.getType().equals(UserType.PROGRAM_CHAIR))
			{
		        User.setCurrentUser(user);
		        my_user = user;
				return true;
			}
			else if (my_admin_btn.isSelected() && user.getName().equals(the_user)
					&& user.getType().equals(UserType.ADMIN))
			{
		        User.setCurrentUser(user);
		        my_user = user;
				return true;
			}
		}
		return false;
	}
}
